//
//  Extensions.m
//  jungoo
//
//  Created by Sinbad Fly on 8/14/12.
//  Copyright (c) 2012 ABOM. All rights reserved.
//

#import "Extensions.h"
#import <QuartzCore/CAAnimation.h>

@implementation UINavigationController (Extension)

- (void)bringUpView:(UIView*)aView inTarget:(id)target
{
    CGRect newRect = aView.frame;
    newRect.origin.y = 20;
    
    [aView setFrame:newRect];
    [self.view addSubview:aView];        
    [aView setBackgroundColor:[UIColor clearColor]];
    
    
    if ([target respondsToSelector:@selector(willBringUpView)]) {
        [target performSelector:@selector(willBringUpView)];
    }    
    
    
    if ([target respondsToSelector:@selector(shouldTapForClosing:)]) {
        
        if ([target respondsToSelector:@selector(view)]) {
            
            UIView *targetView = [target performSelector:@selector(view)];            
            UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] 
                                            initWithTarget:target 
                                            action:@selector(shouldTapForClosing:)] autorelease];
            
            [tap setCancelsTouchesInView:NO];
            [targetView addGestureRecognizer:tap];             
        }   
    }                 
}

- (void)bringUpViewController:(UIViewController*)viewController
{
    CGRect newRect = viewController.view.frame;
    newRect.origin.y = 20;
    
    [viewController.view setFrame:newRect];
    [self.view addSubview:viewController.view];
    [viewController.view setBackgroundColor:[UIColor colorWithWhite:1 alpha:0.75]];
    [viewController viewWillAppear:NO];
    
    if ([viewController respondsToSelector:@selector(willBringUpView)]) {
        [viewController performSelector:@selector(willBringUpView)];
    }    
    
    if ([viewController respondsToSelector:@selector(shouldTapForClosing:)]) {
        UITapGestureRecognizer *tap = [[[UITapGestureRecognizer alloc] 
                                        initWithTarget:viewController 
                                        action:@selector(shouldTapForClosing:)] autorelease];
        [tap setCancelsTouchesInView:NO];
        [viewController.view addGestureRecognizer:tap];    
    }
    
    [viewController retain];
}

- (void)pushViewControllerFade:(UIViewController *)viewController animated:(BOOL)animated
{
    if (animated) {
        CATransition* transition = [CATransition animation];
        transition.duration = 0.3;
        transition.type = kCATransitionFade;
        transition.subtype = kCATransitionFade;
        [self.view.layer addAnimation:transition
                               forKey:kCATransition];
        [self pushViewController:viewController animated:NO];     
    }
    else {
        [self pushViewController:viewController animated:NO];
    }        
}

- (void)popViewControllerFade:(BOOL)animated
{
    if (animated) {
        CATransition* transition = [CATransition animation];
        transition.duration = 0.3;
        transition.type = kCATransitionFade;
        transition.subtype = kCATransitionFade;
        [self.view.layer addAnimation:transition
                               forKey:kCATransition];
        [self popViewControllerAnimated:NO];     
    }
    else {
        [self popViewControllerAnimated:NO];
    }    
}

@end

@implementation UITableViewCell (Extension)

- (UITextField*)fieldTextWithTag:(NSInteger)tag
{
    return (id)[self viewWithTag:tag];
}

- (UILabel*)titleTextWithTag:(NSInteger)tag
{
    return (id)[self viewWithTag:tag];
}

- (UIButton*)buttonWithTag:(NSInteger)tag
{
    return (id)[self viewWithTag:tag];
}

- (UIImageView*)imageViewWithTag:(NSInteger)tag
{
    return (id)[self viewWithTag:tag];
}

- (UITextView*)textViewWithTag:(NSInteger)tag
{
    return (id)[self viewWithTag:tag];
}

@end

@implementation UIView (Extension)

- (void)makeBorderRound
{
    self.clipsToBounds = YES;
    self.layer.cornerRadius = 8;     
}

- (void)setBorderColor:(UIColor*)color
{
    self.layer.borderWidth = 1;
    self.layer.borderColor = [color CGColor];
}

@end

@implementation UITextField(UITextFieldCatagory)

- (CGRect)textRectForBounds:(CGRect)bounds
{
    CGRect theRect = CGRectZero;
    
    if (![self.superview isKindOfClass:[UISearchBar class]]) {
        theRect = CGRectMake(bounds.origin.x + 5, bounds.origin.y, bounds.size.width - 5, bounds.size.height);
    }
    else {
        theRect = CGRectMake(bounds.origin.x + 20, bounds.origin.y, bounds.size.width - 20, bounds.size.height);
    }
    
    return theRect;
}

- (CGRect)editingRectForBounds:(CGRect)bounds
{
    return [self textRectForBounds:bounds];
}

@end

@implementation NSString (Extension)

- (id)objectForKey:(NSString *)key
{
    return nil;
}

@end